<template>
  <el-dialog v-model="dialogVisible" :title="title" width="500px" :append-to-body="true" :before-close="handleClose">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm" status-icon>
      <el-form-item label="姓名" prop="nickName">
        <el-input placeholder="请输入" v-model="ruleForm.nickName" />
      </el-form-item>
      <el-form-item label="手机号/账号" prop="phone">
        <el-input placeholder="请输入" v-model="ruleForm.phone" />
      </el-form-item>
      <!-- <el-form-item label="密码" prop="password" v-if="title === '新增用户'">
        <el-input placeholder="请输入" type="password" v-model="ruleForm.password" />
      </el-form-item> -->
      <el-form-item label="选择角色" prop="isDriver">
        <el-select v-model="ruleForm.isDriver" placeholder="请选择" style="width: 100%">
            <el-option label="管理人员" :value="0" />
            <el-option label="工作人员" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="绑定服务" prop="service">
        <el-select v-model="ruleForm.service" placeholder="请选择" style="width: 100%">
            <el-option :label="item.name" :value="item.id" v-for="item in options" :key="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCloses(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="submit(ruleFormRef)">
          确 认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { REQUEST } from '@/api/request'
const ruleFormRef = ref<FormInstance>()
const title = ref('')
import { store } from '@/store'
const dialogVisible = ref(false)
const emits = defineEmits(['ok'])
const ruleForm = ref({
  id: '',
  nickName: '',
  phone: '',
  password: '',
  houseId: '',
  isDriver: '',
  plateNumber: ''
})
const options = ref([])
const rules = ref({
  nickName: [{ required: true, message: '请输入姓名', trigger: 'change' }],
  phone: [{ required: true, message: '请输入手机号', trigger: 'change' }],
  password: [{ required: true, message: '请输入密码', trigger: 'change' }],
  houseId: [{ required: true, message: '请选择仓库', trigger: 'change' }],
  isDriver: [{ required: true, message: '请选择角色', trigger: 'change' }],
  plateNumber: [{ required: true, message: '请输入车牌号', trigger: 'change' }]
})
const openModal = (t: any, e: any) => {
  console.log(e,'9292929')
  ruleForm.value = {
    id: '',
    nickName: '',
    phone: '',
    password: '',
    houseId: '',
    isDriver: '',
    plateNumber: ''
  }
  dialogVisible.value = true
  title.value = t
  if (e) {
    ruleForm.value = e
  }
}
const handleClose = () => {
  dialogVisible.value = false
  emits('ok')
}
const handleCloses = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emits('ok')
  dialogVisible.value = false
}
const submit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      if(store.state.userInfo?.houseId > 0){
        ruleForm.value.houseId = store.state.userInfo?.houseId
      }
      if (title.value === '新增用户') {

        REQUEST.POST('USERADD', ruleForm.value).then((res: any) => {
          if (res.status === 200) {
            ElMessage({
              type: 'success',
              message: '添加成功！'
            })
            formEl.resetFields()
            dialogVisible.value = false
            emits('ok')
          } else {
            ElMessage({
              type: 'warning',
              message: res.message
            })
          }
        })
      } else {
        REQUEST.POST('USEREDIT', ruleForm.value).then((res: any) => {
          if (res.status === 200) {
            ElMessage({
              type: 'success',
              message: '设置成功！'
            })
            dialogVisible.value = false
            formEl.resetFields()
            emits('ok')
          } else {
            ElMessage({
              type: 'warning',
              message: res.message
            })
          }
        })

      }
    }
  })
}
defineExpose({ openModal })
</script>

<style lang="" scoped></style>
